package cn.xu.tool.big.a.compments;

import cn.xu.tool.big.a.dto.TradeRecord;
import cn.xu.tool.big.a.util.FileUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 类名: TradeRecordCp
 * 描述: TODO
 * 作者: ClarkXu clark2021@qq.com
 * 日期: 2025/8/5
 */
public class TradeRecordCp {

    public static void saveRecordFile(String parentPath, String stockCode, List<TradeRecord> results, String startDate, String endDate) throws IOException {
        String dirPath = parentPath + "/backtest/"+stockCode;
        File dir = new File(dirPath);
        if(!dir.exists()){
            dir.mkdir();
        }
        String outFile = dirPath+"/"+startDate+"-"+endDate+"_record.txt";
        FileUtil.write(JSON.toJSONString(results), outFile);
    }

    public static List<TradeRecord> readRecordFile(String parentPath, String stockCode, String startDate, String endDate) throws IOException {
        String dirPath = parentPath + "/backtest/"+stockCode;
        File dir = new File(dirPath);
        if(!dir.exists()){
            dir.mkdir();
        }
        String outFile = dirPath+"/"+startDate+"-"+endDate+"_record.txt";
        if(!new File(outFile).exists()){
            return null;
        }
        String recordJsons = FileUtil.read(outFile);
        if(StringUtils.isBlank(recordJsons)){
            return null;
        }
        return JSON.parseArray(recordJsons, TradeRecord.class);
    }
}
